package com.ims.task;

import com.ims.common.core.redis.RedisCache;
import com.ims.common.utils.DateUtils;
import com.ims.common.utils.StringUtils;
import com.ims.core.doc.service.ITInDeductionStatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 统计表 task任务
 */
@Component("DeductionStatTask")
public class DeductionStatTask {
    //日志
    private static final Logger logger = LoggerFactory.getLogger(DeductionStatTask.class);

    /**
     * 统计表生成/取消结果查询任务KEY
     */
    public static final String STAT_APPLY_RESULT_TASK_KEY = "task_stat_apply_result";

    /**
     * 统计表生成/取消结果查询任务生命周期KEY
     */
    public static final String STAT_APPLY_RESULT_TASK_TTL_KEY = "task_stat_apply_result_ttl";

    /**
     * 统计表签名/取消结果查询任务KEY
     */
    public static final String STAT_CONFIRM_APPLY_RESULT_TASK_KEY = "task_stat_confirm_apply_result";

    /**
     * 统计表签名/取消结果查询任务生命周期KEY
     */
    public static final String STAT_CONFIRM_APPLY_RESULT_TASK_TTL_KEY = "task_stat_confirm_apply_result_ttl";

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ITInDeductionStatService tInDeductionStatService;

    /**
     * 动态任务执行
     * 生成一个统计表
     */
    // @Scheduled(cron = "0 0/3 * * * ?")
    public void autoCreateInDeductionStat() {
        // cron : 0 */1 * * * ?
        logger.info("---autoCreateInDeductionStat task------produce Data with fixed rate task: Execution Time - {}", DateUtils.getNowDate());
        try {
            tInDeductionStatService.autoCreateInDeductionStatForNextPeriod();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("autoCreateInDeductionStatApply.task" + " | msg : " + e.getMessage());
        }
    }

    /**
     * 查询统计表生成/撤销结果
     */
    // @Scheduled(cron = "0 0/3 * * * ?")
    public void syncDeductionStatApplyResult() {
        // cron : 0 */1 * * * ?
        logger.info("---syncDeductionStatApplyResult task------produce Data with fixed rate task: Execution Time - {}", DateUtils.getNowDate());
        try {
            // 查询redis中统计表生成/取消结果查询任务生命周期
            if(!redisCache.hasKey(STAT_APPLY_RESULT_TASK_TTL_KEY)){
                // 获取统计表确认申请ID
                String applyId = (String)redisCache.getCacheObject(STAT_APPLY_RESULT_TASK_KEY);
                if(!StringUtils.isEmpty(applyId)){
                    tInDeductionStatService.syncDeductionStatApplyResultByApplyId(applyId);
                } else{
                    logger.warn("查询统计表生成/撤销结果任务已移除");
                }
                // 移除查询任务
                redisCache.deleteObject(STAT_APPLY_RESULT_TASK_KEY);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("syncDeductionStatApplyResult.task" + " | msg : " + e.getMessage());
        }
    }


    /**
     * 查询统计表确认/取消结果
     */
    // @Scheduled(cron = "0 0/3 * * * ?")
    public void syncDeductionStatConfirmApplyResult() {
        logger.info("---syncDeductionStatConfirmApplyResult task------produce Data with fixed rate task: Execution Time - {}", DateUtils.getNowDate());
        try {
            // 查询redis中统计表签名确认/取消结果查询任务生命周期
            if(!redisCache.hasKey(STAT_CONFIRM_APPLY_RESULT_TASK_TTL_KEY)){
                // 获取统计表申请ID
                String applyId = (String)redisCache.getCacheObject(STAT_CONFIRM_APPLY_RESULT_TASK_KEY);
                if(!StringUtils.isEmpty(applyId)){
                    tInDeductionStatService.syncDeductionStatConfirmApplyResultByApplyId(applyId);
                } else {
                    logger.warn("查询统计表确认/取消结果任务已移除");
                }
                // 移除查询任务
                redisCache.deleteObject(STAT_CONFIRM_APPLY_RESULT_TASK_KEY);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("syncDeductionStatConfirmApplyResult.task" + " | msg : " + e.getMessage());
        }
    }


}
